﻿using Prism.Commands;
using Prism.Mvvm;
using System.Windows.Input;
using System;
using System.Windows;
using MyWPF.Views;
using System.Linq;
using System.Windows.Controls;
using MyWPF.StaticData;
using MyWPF.Models;
using System.Windows.Media;
using MyWPF.Views.Others;
using LiveCharts;
using LiveCharts.Configurations;
using System.Threading.Tasks;
using LiveCharts.Defaults;
using System.Collections.Generic;
using System.Windows.Media.TextFormatting;
using System.Windows.Threading;
using System.Threading;
using WPFDevelopers.Controls;
using CommonFunction;

namespace MyWPF.ViewModel
{
    public class VM_MainWindow : BaseViewModel
    {
        public VM_MainWindow()
        {
            #region 事件委托
            _close = new DelegateCommand<object>(_Close);
            _min = new DelegateCommand<object>(_Min);
            _moveWindow = new DelegateCommand<object>(_MoveWindow);
            _openPCState = new DelegateCommand<object>(_OpenPCState);
            _screenshot = new DelegateCommand<object>(_Screenshot);
            _windowSwitching = new DelegateCommand<object>(_WindowSwitching);
            _openSetWindow = new DelegateCommand<object>(_OpenSetWindow);
            _openUserWindow = new DelegateCommand<object>(_OpenUserWindow);
            _removeNews = new DelegateCommand<object>(_RemoveNews);
            _mainPage = new DelegateCommand<object>(_MainPage);
            _journalPage = new DelegateCommand<object>(_JournalPage);
            _recipePage = new DelegateCommand<object>(_RecipePage);
            _curvePage = new DelegateCommand<object>(_CurvePage);
            #endregion

            WindowIcon = System.Net.WebUtility.HtmlDecode("&#xeb11;");
            IconSize = 30;
            StaticModels.mainPage = new Views.Subpage.MainPage();
            Content = StaticModels.mainPage;


            
        }
        #region 基础：前端事件
        /// <summary>
        /// 关闭窗口
        /// </summary>
        private ICommand _close;
        public ICommand Close
        {
            get { return _close; }
            set
            {
                _close = value;
                OnPropertyChanged("Close");
            }
        }
        private void _Close(object obj)
        {
            ConfirmationWindow confirmationWindow = new ConfirmationWindow("是否退出？");
            bool Choice = (bool)confirmationWindow.ShowDialog();
            if (Choice)
            {
                StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                {
                    Context = "用户主动退出登录",
                    Time = DateTime.Now,
                    Type = Models.Journal.JournalType.日志,
                    User = StaticModels.user.User.UserName
                });
                StaticModels.mainWindow.Close();
                System.Environment.Exit(0);
            }

        }
        /// <summary>
        /// 窗口状态切换
        /// </summary>
        private ICommand _windowSwitching;
        public ICommand WindowSwitching
        {
            get { return _windowSwitching; }
            set
            {
                _windowSwitching = value;
                OnPropertyChanged("WindowSwitching");
            }
        }
        private void _WindowSwitching(object obj)
        {
            //获取窗口实例化
            var _mainWindow = StaticModels.mainWindow;
            if (_mainWindow.WindowState == System.Windows.WindowState.Normal)
            {
                WindowIcon = System.Net.WebUtility.HtmlDecode("&#xe608;");
                _mainWindow.WindowState = System.Windows.WindowState.Maximized;
                _mainWindow.ShowInTaskbar = false;
                IconSize = 20;
                _mainWindow.Hide();
                _mainWindow.Show();
            }
            else
            {
                WindowIcon = System.Net.WebUtility.HtmlDecode("&#xeb11;");
                _mainWindow.WindowState = System.Windows.WindowState.Normal;
                IconSize = 30;
            }
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        private ICommand _min;
        public ICommand Min
        {
            get { return _min; }
            set
            {
                _min = value;
                OnPropertyChanged("Min");
            }
        }
        private void _Min(object obj)
        {
            StaticModels.mainWindow.WindowState = System.Windows.WindowState.Minimized;
        }
        /// <summary>
        /// 移动窗口
        /// </summary>
        private ICommand _moveWindow;
        public ICommand MoveWindow
        {
            get { return _moveWindow; }
            set
            {
                _moveWindow = value;
                OnPropertyChanged("MoveWindow");
            }
        }
        private void _MoveWindow(object obj)
        {
            StaticModels.mainWindow.DragMove();
        }

        #endregion
        #region 基础：页面切换
        private UserControl _content;
        public UserControl Content
        {
            get { return _content; }
            set { SetProperty(ref _content, value); }
        }
        /// <summary>
        /// 切换到首页
        /// </summary>
        private ICommand _mainPage;
        public ICommand MainPage
        {
            get { return _mainPage; }
            set
            {
                _mainPage = value;
                OnPropertyChanged("MainPage");
            }
        }
        private void _MainPage(object obj)
        {
            ExecutePage(MainPages.MainPage);
        }
        /// <summary>
        /// 切换到日志
        /// </summary>
        private ICommand _journalPage;
        public ICommand JournalPage
        {
            get { return _journalPage; }
            set
            {
                _journalPage = value;
                OnPropertyChanged("JournalPage");
            }
        }
        private void _JournalPage(object obj)
        {
            ExecutePage(MainPages.JournalPage);
        }
        /// <summary>
        /// 切换到配方
        /// </summary>
        private ICommand _recipePage;
        public ICommand RecipePage
        {
            get { return _recipePage; }
            set
            {
                _recipePage = value;
                OnPropertyChanged("RecipePage");
            }
        }
        private void _RecipePage(object obj)
        {
            ExecutePage(MainPages.RecipePage);
        }
        /// <summary>
        /// 切换到曲线
        /// </summary>
        private ICommand _curvePage;
        public ICommand CurvePage
        {
            get { return _curvePage; }
            set
            {
                _curvePage = value;
                OnPropertyChanged("CurvePage");
            }
        }
        private void _CurvePage(object obj)
        {
            ExecutePage(MainPages.CurvePage);
        }

        public void ExecutePage(MainPages page)
        {
            switch (page)
            {
                case MainPages.MainPage:
                    StaticModels.mainPage = new Views.Subpage.MainPage();
                    Content = StaticModels.mainPage;
                    break;
                case MainPages.JournalPage:
                    StaticModels.journalPage = new Views.Subpage.JournalPage();
                    Content = StaticModels.journalPage;
                    break;
                case MainPages.RecipePage:
                    StaticModels.recipePage = new Views.Subpage.RecipePage();
                    Content = StaticModels.recipePage;
                    break;
                case MainPages.CurvePage:
                    StaticModels.curvePage = new Views.Subpage.CurvePage();
                    Content = StaticModels.curvePage;
                    break;

            }
        }
        private void OnPropertyChanged(string v)
        {
            throw new NotImplementedException();
        }
        #endregion


        #region 属性绑定：界面信息绑定
        private string _winCCName;
        public string WinCCName
        {
            get { return _winCCName; }
            set { SetProperty(ref _winCCName, value); }
        }
        private string _windowIcon;
        public string WindowIcon
        {
            get { return _windowIcon; }
            set { SetProperty(ref _windowIcon, value); }
        }
        private int _iconSize;
        public int IconSize
        {
            get { return _iconSize; }
            set { SetProperty(ref _iconSize, value); }
        }
        private int _ambiguity;
        public int Ambiguity
        {
            get { return _ambiguity; }
            set { SetProperty(ref _ambiguity, value); }
        }
        #endregion

        #region 属性绑定：右下角消息提示绑定
        public int NewsNum
        {
            get { return _newsNum; }
            set 
            {
                if (value > 0)
                    NewsVisibility = Visibility.Visible;
                else
                {
                    NewsVisibility = Visibility.Hidden;
                    NewsCheck = false;
                }
                    
                SetProperty(ref _newsNum, value);
            }
        }
        private int _newsNum;

        public Visibility NewsVisibility
        {
            get { return _newsVisibility; }
            set { SetProperty(ref _newsVisibility, value); }
        }
        private Visibility _newsVisibility;


        public bool NewsCheck
        {
            get { return _newsCheck; }
            set { SetProperty(ref _newsCheck, value); }
        }
        private bool _newsCheck;
        #endregion
        
        #region 事件绑定：右键打开电脑监控窗口，截屏
        /// <summary>
        /// 打开电脑监控窗口
        /// </summary>
        private ICommand _openPCState;
        public ICommand OpenPCState
        {
            get { return _openPCState; }
            set
            {
                _openPCState = value;
                OnPropertyChanged("OpenPCState");
            }
        }
        private void _OpenPCState(object obj)
        {
            StaticModels.pCState = new Views.RightClick.PCState();
            StaticModels.pCState.Show();
        }
        /// <summary>
        /// 截屏
        /// </summary>
        private ICommand _screenshot;
        public ICommand Screenshot
        {
            get { return _screenshot; }
            set
            {
                _screenshot = value;
                OnPropertyChanged("Screenshot");
            }
        }
        private void _Screenshot(object obj)
        {
            var screenCut = new ScreenCut();
            screenCut.ShowDialog();
        }
        #endregion

        #region 事件绑定：打开其它窗口
        /// <summary>
        /// 打开设置窗体
        /// </summary>
        private ICommand _openSetWindow;
        public ICommand OpenSetWindow
        {
            get { return _openSetWindow; }
            set
            {
                _openSetWindow = value;
                OnPropertyChanged("OpenSetWindow");
            }
        }
        private void _OpenSetWindow(object obj)
        {
            StaticModels.setWindow = new Views.Setting.SetWindow();
            Ambiguity = 5;
            StaticModels.setWindow.ShowDialog();
            Ambiguity = 0;
        }
        /// <summary>
        /// 打开设置窗体
        /// </summary>
        private ICommand _openUserWindow;
        public ICommand OpenUserWindow
        {
            get { return _openUserWindow; }
            set
            {
                _openUserWindow = value;
                OnPropertyChanged("OpenUserWindow");
            }
        }
        private void _OpenUserWindow(object obj)
        {
            try
            {
                StaticModels.vM_UserWindow.UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);
                Ambiguity = 5;
                StaticModels.userWindow.ShowDialog();
                Ambiguity = 0;
            }
            catch
            {
                StaticModels.vM_UserWindow.UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);
                StaticModels.userWindow = new Views.User.UserWindow();
                Ambiguity = 5;
                StaticModels.userWindow.ShowDialog();
                Ambiguity = 0;
            }
        }
        

        #endregion

        #region 事件绑定：右下角移除News
        /// <summary>
        /// 移除News
        /// </summary>
        private ICommand _removeNews;
        public ICommand RemoveNews
        {
            get { return _removeNews; }
            set
            {
                _removeNews = value;
                OnPropertyChanged("RemoveNews");
            }
        }
        private void _RemoveNews(object obj)
        {
            StaticModels.mainWindow.NewsRoom.Children.Clear();
            StaticModels.vM_MainWindow.NewsNum = 0;
        }
        #endregion
    }
    public enum MainPages
    {
        MainPage, JournalPage, RecipePage, CurvePage
    }
}
